const $ = require('selenium-query');

const login = browser => (email, password) => {
    browser.get('https://www.udemy.com');
    const $browser = $(browser);

    const getLoginModal = () => $browser.find('#id_email').attr('name')
            .then((emailInput) => {
                if (!emailInput) {
                    return getLoginModal();
                }

                return emailInput;
            });

    return $browser.find('.dropdown--login').click()
        .then(() => getLoginModal())
        .then(() => $browser.find('#id_email').val(email)
                .then(() => $browser.find('#id_password').val(password))
                .then(() => $browser.find('#submit-id-submit').click()));
};

module.exports = login;
